package com.qqbot.impl.config;

import com.qqbot.impl.interceptor.SignatureInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.LinkedList;

/**
 * 注册拦截器
 *
 * @author changwenpeng
 * @date 2022年5月28日
 */
@Configuration
public class MvcConfigurer implements WebMvcConfigurer {

    @Bean
    public SignatureInterceptor signatureInInterceptor() {
        return new SignatureInterceptor();
    }

    /**
     * 注册拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LinkedList<String> addPathList = new LinkedList<>();
        addPathList.add("/open/api/**");
        registry.addInterceptor(signatureInInterceptor())
                .addPathPatterns(addPathList);
    }

    /**
     * 解决跨域问题
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                //.allowCredentials(true)
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(3600);
    }
}